package com.demo.dao;

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.demo.domain.TeamComposition;
import org.apache.ibatis.annotations.*;

import java.util.List;

@Mapper
public interface TeamCompositionDao extends BaseMapper<TeamComposition> {

//    删除对应队伍成员
    @Delete("DELETE FROM tb_team_composition tc WHERE tc.tc_team_id=#{teamId} and tc.tc_urealname=#{urealname} and tc.tc_uid=#{uid}")
    int deleteTeamMember(Long teamId,String urealname,Long uid);

    //查找报名用户是否已存在于报名的队伍中
    @Select("SELECT EXISTS(SELECT 1 FROM tb_team_composition tc WHERE tc.tc_uid=#{uid} AND tc.tc_team_Id=#{teamId})")
    boolean getUserInfo(Long uid,Long teamId);

    @Select("SELECT tc_member_type FROM tb_team_composition tc WHERE tc.tc_uid=#{uid} and tc.tc_team_Id=#{teamId} and tc.tc_del_flag = 0")
    Integer getUserType(Long uid,Long teamId);

    @Update("UPDATE tb_team_composition tc set tc.tc_del_flag = 1 where tc.tc_id = #{id}")
    int deleteTeamCompositionById(TeamComposition teamComposition);

    @Select("SELECT tc_id,tc_uid,tc_urealname,tc_team_id,tc_member_type,tc_del_flag FROM tb_team_composition tc where tc.tc_del_flag = 0")
    @Results({
            @Result(column = "tc_id", property = "id"),
            @Result(column = "tc_uid", property = "uid"),
            @Result(column = "tc_urealname", property = "urealname"),
            @Result(column = "tc_team_id", property = "teamId"),
            @Result(column = "tc_member_type", property = "memberType"),
            @Result(column = "tc_del_flag", property = "delFlag")
    })
    List<TeamComposition> listAllTeamComposition();


    @Select("<script>" +
            "SELECT tc_id,tc_uid,tc_urealname,tc_team_id,tc_member_type,tc_del_flag FROM tb_team_composition tc " +
            "where tc.tc_del_flag = 0" +
            "<if test='uid != null and uid != \"\"'>AND tc.tc_uid = #{uid}</if>" +
            "<if test='urealname != null and urealname != \"\"'>AND tc.tc_urealname = #{urealname}</if>" +
            "<if test='teamId != null and teamId != \"\"'>AND tc.tc_team_id = #{teamId}</if>" +
            " ORDER BY tc.tc_id ASC" +
            "</script>")
    @Results({
            @Result(column = "tc_id", property = "id"),
            @Result(column = "tc_uid", property = "uid"),
            @Result(column = "tc_urealname", property = "urealname"),
            @Result(column = "tc_team_id", property = "teamId"),
            @Result(column = "tc_member_type", property = "memberType"),
            @Result(column = "tc_del_flag", property = "delFlag")
    })
    List<TeamComposition> selectTeamCompositionList(Long uid, String urealname, Long teamId);

}
